package me.cxlr.qinlauncher2.view.launcher;

import android.content.Context;
import android.view.View;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.FragmentActivity;

import me.cxlr.qinlauncher2.constants.Constants;
import me.cxlr.qinlauncher2.dao.KeyActionDao;
import me.cxlr.qinlauncher2.manager.ActionManager;
import me.cxlr.qinlauncher2.manager.SoftwareManager;
import me.cxlr.qinlauncher2.model.KeyAction;
import me.cxlr.qinlauncher2.util.ResourceUtil;
import me.cxlr.qinlauncher2.util.SharedPreferencesUtil;

/**
 * 物理按键
 *
 * @author 酷安用户:山轮纸
 */
public class FunctionKey {
    private volatile static FunctionKey functionKey;

    private FunctionKey() {
        keyActionDao = new KeyActionDao();
    }

    public static FunctionKey getInstance() {
        if (functionKey == null) {
            synchronized (FunctionKey.class) {
                if (functionKey == null) {
                    functionKey = new FunctionKey();
                }
            }
        }
        return functionKey;
    }

    private KeyActionDao keyActionDao;

    /**
     * 左屏幕虚拟功能键
     *
     * @param tvLeftVirtualFunctionKey 组件
     */
    public void leftVirtualFunctionKey(FragmentActivity activity, AppCompatTextView tvLeftVirtualFunctionKey) {
        // 左屏幕虚拟功能键
        boolean leftVirtualFunctionKeyFlag = SharedPreferencesUtil.getInstance().getBoolean("left_virtual_function_key", true);
        if (leftVirtualFunctionKeyFlag) {
            KeyAction leftFunctionKeyAction = keyActionDao.findKeyActionByKeyNameAndKeyType(Constants.FUNCTION_KEY_MENU, Constants.KEY_TYPE_FUNCTION_KEY);
            tvLeftVirtualFunctionKey.setVisibility(View.VISIBLE);

            boolean flagUseLeftVirtualKeyDiyWord = SharedPreferencesUtil.getInstance().getBoolean("use_left_virtual_function_key_diy_word", false);
            if (flagUseLeftVirtualKeyDiyWord) {
                String leftVirtualKeyDiyWork = SharedPreferencesUtil.getInstance().getString("left_virtual_function_key_diy_word", "");
                tvLeftVirtualFunctionKey.setText(leftVirtualKeyDiyWork);
            } else {
                if (leftFunctionKeyAction.getAction() != null) {
                    String resLabel = leftFunctionKeyAction.getAction().getResLabel();
                    tvLeftVirtualFunctionKey.setText(ResourceUtil.getString(activity, resLabel));
                } else if (leftFunctionKeyAction.getApp() != null) {
                    tvLeftVirtualFunctionKey.setText(leftFunctionKeyAction.getApp().getLabel());
                } else {
                    tvLeftVirtualFunctionKey.setText("");
                }
            }
            tvLeftVirtualFunctionKey.setOnClickListener(view -> {
                switch (leftFunctionKeyAction.getActionType()) {
                    case Constants.KEY_ACTION_TYPE_OPEN_APP:
                        SoftwareManager.getInstance().openApplication(activity, view.getContext(), leftFunctionKeyAction.getApp().getPackageName(), leftFunctionKeyAction.getApp().getClazzName());
                        break;
                    case Constants.KEY_ACTION_TYPE_ACTION:
                        ActionManager.getInstance().openAction(activity, view.getContext(), leftFunctionKeyAction.getAction().getActionName());
                        break;
                    default:
                        break;
                }
            });
        } else {
            tvLeftVirtualFunctionKey.setVisibility(View.GONE);
        }
    }

    /**
     * 右屏幕虚拟功能键
     *
     * @param tvRightVirtualFunctionKey 组件
     */
    public void rightVirtualFunctionKey(FragmentActivity activity, AppCompatTextView tvRightVirtualFunctionKey) {
        // 右屏幕虚拟功能键
        boolean rightVirtualFunctionKeyFlag = SharedPreferencesUtil.getInstance().getBoolean("right_virtual_function_key", true);
        if (rightVirtualFunctionKeyFlag) {
            KeyAction rightFunctionKeyAction = keyActionDao.findKeyActionByKeyNameAndKeyType(Constants.FUNCTION_KEY_BACK, Constants.KEY_TYPE_FUNCTION_KEY);
            tvRightVirtualFunctionKey.setVisibility(View.VISIBLE);

            boolean flagUseRightVirtualKeyDiyWord = SharedPreferencesUtil.getInstance().getBoolean("use_right_virtual_function_key_diy_word", false);
            if (flagUseRightVirtualKeyDiyWord) {
                String rightVirtualKeyDiyWork = SharedPreferencesUtil.getInstance().getString("right_virtual_function_key_diy_word", "");
                tvRightVirtualFunctionKey.setText(rightVirtualKeyDiyWork);
            } else {
                if (rightFunctionKeyAction.getAction() != null) {
                    String resLabel = rightFunctionKeyAction.getAction().getResLabel();
                    tvRightVirtualFunctionKey.setText(ResourceUtil.getString(activity, resLabel));
                } else if (rightFunctionKeyAction.getApp() != null) {
                    tvRightVirtualFunctionKey.setText(rightFunctionKeyAction.getApp().getLabel());
                } else {
                    tvRightVirtualFunctionKey.setText("");
                }
            }

            tvRightVirtualFunctionKey.setOnClickListener(view -> {
                switch (rightFunctionKeyAction.getActionType()) {
                    case Constants.KEY_ACTION_TYPE_OPEN_APP:
                        SoftwareManager.getInstance().openApplication(activity, view.getContext(), rightFunctionKeyAction.getApp().getPackageName(), rightFunctionKeyAction.getApp().getClazzName());
                        break;
                    case Constants.KEY_ACTION_TYPE_ACTION:
                        ActionManager.getInstance().openAction(activity, view.getContext(), rightFunctionKeyAction.getAction().getActionName());
                        break;
                    default:
                        break;
                }
            });
        } else {
            tvRightVirtualFunctionKey.setVisibility(View.GONE);
        }
    }

    /**
     * 左功能键菜单键单击
     */
    public boolean leftPhysicalFunctionKey(FragmentActivity activity, Context context) {
        // 左功能键
        boolean leftPhysicalFunctionKey = SharedPreferencesUtil.getInstance().getBoolean("left_physical_function_key", true);
        if (leftPhysicalFunctionKey) {
            KeyAction keyAction = keyActionDao.findKeyActionByKeyNameAndKeyType(Constants.FUNCTION_KEY_MENU, Constants.KEY_TYPE_FUNCTION_KEY);
            switch (keyAction.getActionType()) {
                // 打开应用
                case Constants.KEY_ACTION_TYPE_OPEN_APP:
                    SoftwareManager.getInstance().openApplication(activity, context, keyAction.getApp().getPackageName(), keyAction.getApp().getClazzName());
                    break;
                // 打开动作
                case Constants.KEY_ACTION_TYPE_ACTION:
                    ActionManager.getInstance().openAction(activity, context, keyAction.getAction().getActionName());
                    break;
                default:
                    break;
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 左功能键菜单键长按
     */
    public boolean leftPhysicalFunctionKeyLongClick(FragmentActivity activity, Context context) {
        KeyAction keyAction = keyActionDao.findKeyActionByKeyNameAndKeyType(Constants.FUNCTION_KEY_MENU_LONG, Constants.KEY_TYPE_FUNCTION_KEY);
        switch (keyAction.getActionType()) {
            // 打开应用
            case Constants.KEY_ACTION_TYPE_OPEN_APP:
                SoftwareManager.getInstance().openApplication(activity, context, keyAction.getApp().getPackageName(), keyAction.getApp().getClazzName());
                break;
            // 打开动作
            case Constants.KEY_ACTION_TYPE_ACTION:
                ActionManager.getInstance().openAction(activity, context, keyAction.getAction().getActionName());
                break;
            default:
                break;
        }
        return true;

    }

    /**
     * 右功能键菜单键单击
     */
    public boolean rightPhysicalFunctionKey(FragmentActivity activity, Context context) {
        // 右功能键
        boolean rightPhysicalFunctionKey = SharedPreferencesUtil.getInstance().getBoolean("right_physical_function_key", true);
        if (rightPhysicalFunctionKey) {
            KeyAction keyAction = keyActionDao.findKeyActionByKeyNameAndKeyType(Constants.FUNCTION_KEY_BACK, Constants.KEY_TYPE_FUNCTION_KEY);
            switch (keyAction.getActionType()) {
                // 打开应用
                case Constants.KEY_ACTION_TYPE_OPEN_APP:
                    SoftwareManager.getInstance().openApplication(activity, context, keyAction.getApp().getPackageName(), keyAction.getApp().getClazzName());
                    break;
                // 打开动作
                case Constants.KEY_ACTION_TYPE_ACTION:
                    ActionManager.getInstance().openAction(activity, context, keyAction.getAction().getActionName());
                    break;
                default:
                    break;
            }
            return true;
        } else {
            return false;
        }
    }
}
